package com.shunlin.paopaoku.box2d;

import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.physics.box2d.Body;
import com.shunlin.paopaoku.game.*;

//绘制类

public class Box2DSprite {
    public Animation animation; //动画类
    public Body body; //刚体类
    private Player player; //自定义的玩家类里边封装有三个动画，分别为左走分别为左走，右走，站立
    public int width, height;

    public Box2DSprite(Body body) {
        this.body = body;

        ClassManager.player.body = body; //从ClassManager里边调用body防止调用的不是同一个对象,造成空指针异常
        player = ClassManager.player; //从ClassManage从class manager里边调用player

        width = player.getAnimationWidth(); //调用player里的getAnimation方法拿到图片的宽和高
        height = player.getAnimationHeight();
    }

    public void render(SpriteBatch sb, float deltaTime) { //渲染方法会无限调用
        player.update(deltaTime);

        animation = player.animation;

        sb.begin(); //开始绘制

        sb.draw(animation.getKeyFrame(deltaTime), //调用Animation里边的方法拿到关键帧进行绘制
                body.getPosition().x * Box2DVars.PPM - width / 2, //重新设置位置使player的位置等于body的位置
                body.getPosition().y * Box2DVars.PPM - height / 2);

        //减去宽和高再除以二的原因是因为Box2D的坐标锚点在屏幕正中间，而Android的坐标锚点在屏幕的左下角，所以要进行一个换算

        sb.end(); //结束绘制
    }
}
